/**
 * @Author: liutao
 * @Date:   2017-04-19T17:01:26+08:00
 * @Email:  947809647@qq.com
 * @Last modified by:   liutao
 * @Last modified time: 2017-04-25T16:04:48+08:00
 */
import page from '../../../pages/pages.vue'
import { getGameList, auditRecord, reAuditRecord, noAward, getSearchGameList, showMsg } from '../../../../vuex/action.js'

export default {
  data() {
    return {
      hasMore: true,
      list: [],
      status: 3,
      ids: [],
      phone: '',
      pages: {
        start: 0,
        limit: 100
      },
      selectAll: false
    }
  },
  methods: {
    evtGetGameList() {
      this.pages.start = 0
      this.xGetGameList({
        start: this.pages.start,
        limit: this.pages.limit,
        status: this.status
      }).then((res) => {
        this.list = res.data.list
        this.pages.start = this.list.length
        if (res.data.list && res.data.list.length > 0) {
          this.hasMore = true
        } else {
          this.hasMore = false
        }
      }).catch((res) => {
        this.xShowMsg({
          type: 'danger',
          content: res.error_msg
        })
      })
    },
    evtGetMoreGameList() {
      this.xGetGameList({
        start: this.pages.start,
        limit: this.pages.limit,
        status: this.status
      }).then((res) => {
        if (res.data.list && res.data.list.length > 0) {
          this.list = this.list.concat(res.data.list)
          this.pages.start = this.list.length
          this.hasMore = true
        } else {
          this.hasMore = false
        }
      }).catch((res) => {
        this.xShowMsg({
          type: 'danger',
          content: res.error_msg
        })
      })
    },
    evtGetSearchGameList() {
      this.xGetSearchGameList({
        phone: this.phone
      }).then((res) => {
        this.list = res.data.game
      }).catch((res) => {
        this.xShowMsg({
          type: 'danger',
          content: res.error_msg
        })
      })
    },
    evtLotsizeAudit() {
      this.xAuditRecord({
        ids: this.ids.join()
      }).then((res) => {
        this.evtGetGameList()
      }).catch((res) => {
        this.xShowMsg({
          type: 'danger',
          content: res.error_msg
        })
      })
    },
    evtLotsizeNoAward() {
      this.xNoAward({
        ids: this.ids.join()
      }).then((res) => {
        this.evtGetGameList()
      }).catch((res) => {
        this.xShowMsg({
          type: 'danger',
          content: res.error_msg
        })
      })
    },
    evtLotsizeReAuditRecord() {
      this.xReAuditRecord({
        ids: this.ids.join()
      }).then((res) => {
        this.evtGetGameList()
      }).catch((res) => {
        this.xShowMsg({
          type: 'danger',
          content: res.error_msg
        })
      })
    },
    evtNoAward(id) {
      let ids = [id]
      this.xNoAward({
        ids: ids.join()
      }).then((res) => {
        this.evtGetGameList()
      }).catch((res) => {
        this.xShowMsg({
          type: 'danger',
          content: res.error_msg
        })
      })
    },
    evtAuditRecord(id) {
      let ids = [id]
      this.xAuditRecord({
        ids: ids.join()
      }).then((res) => {
        this.evtGetGameList()
      }).catch((res) => {
        this.xShowMsg({
          type: 'danger',
          content: res.error_msg
        })
      })
    },
    evtReAuditRecord(id) {
      let ids = [id]
      this.xReAuditRecord({
        ids: ids.join()
      }).then((res) => {
        this.evtGetGameList()
      }).catch((res) => {
        this.xShowMsg({
          type: 'danger',
          content: res.error_msg
        })
      })
    }
  },
  watch: {
    selectAll(newVal) {
      if (newVal) {
        this.list.forEach((item) => {
          if (+item.status === +this.status) {
            this.ids.push(item.id)
          }
        })
      } else {
        this.ids = []
      }
    },
    status() {
      this.evtGetGameList()
    }
  },
  vuex: {
    actions: {
      xGetGameList: getGameList,
      xAuditRecord: auditRecord,
      xReAuditRecord: reAuditRecord,
      xNoAward: noAward,
      xGetSearchGameList: getSearchGameList,
      xShowMsg: showMsg
    }
  },
  components: {
    'vue-page': page
  },
  ready() {
    this.evtGetGameList()
  }
}
